package com.luyuan.yuansousuo.manager;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luyuan.yuansousuo.common.ErrorCode;
import com.luyuan.yuansousuo.datasource.PictureDataSource;
import com.luyuan.yuansousuo.datasource.PostDataSource;
import com.luyuan.yuansousuo.datasource.UserDataSource;
import com.luyuan.yuansousuo.exception.BusinessException;
import com.luyuan.yuansousuo.exception.ThrowUtils;
import com.luyuan.yuansousuo.model.dto.post.PostQueryRequest;
import com.luyuan.yuansousuo.model.dto.search.SearchRequestParam;
import com.luyuan.yuansousuo.model.dto.user.UserQueryRequest;
import com.luyuan.yuansousuo.model.entity.Picture;
import com.luyuan.yuansousuo.model.enums.SearchTypeEnum;
import com.luyuan.yuansousuo.model.vo.PostVO;
import com.luyuan.yuansousuo.model.vo.SearchVO;
import com.luyuan.yuansousuo.model.vo.UserVO;
import com.luyuan.yuansousuo.service.PostService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.CompletableFuture;

@Component
@Slf4j
public class SearchFade {

    @Resource
    private PostDataSource postDataSource;
    @Resource
    private UserDataSource userDataSource;
    @Resource
    private PictureDataSource pictureDataSource;




    public SearchVO searchAll(SearchRequestParam searchRequest, HttpServletRequest request) {
        log.info("search all request:{}", searchRequest);

        // 获取请求参数查询关键词
        String searchText = searchRequest.getSearchText();
        // 获取请求参数中的页数
        long current = searchRequest.getCurrent();
        // 获取请求参数中的每页大小
        long pageSize = searchRequest.getPageSize();
        // 获取请求参数中的类型
        String type = searchRequest.getType();
        SearchTypeEnum searchTypeEnum = SearchTypeEnum.getEnumByValue(type);

        // 构造返回结果
        SearchVO searchVO = new SearchVO();
        // 利用自定义抛异常工具类 判断 tpye 是否合法
        ThrowUtils.throwIf(StringUtils.isBlank(type), ErrorCode.PARAMS_ERROR);
        // 如果 type 为空 查询所有类型数据
        if (searchTypeEnum == null) {
            // 查询用户数据
            CompletableFuture<Page<UserVO>> userTask = CompletableFuture.supplyAsync(() -> {
                Page<UserVO> userVOPage = userDataSource.doSearch(searchText, current, pageSize);
                return userVOPage;
            });
            // 查询帖子数据
            CompletableFuture<Page<PostVO>> postTask = CompletableFuture.supplyAsync(() -> {
                Page<PostVO> postVOPage = postDataSource.doSearch(searchText, current, pageSize);
                return postVOPage;
            });
            // 查询图片数据
            CompletableFuture<Page<Picture>> pictureTask = CompletableFuture.supplyAsync(() -> {
                Page<Picture> picture = pictureDataSource.doSearch(searchText, current, pageSize);
                return picture;
            });
            // 等待所有任务完成
            CompletableFuture.allOf(userTask, postTask, pictureTask).join();

            try {
                Page<UserVO> userVOPage = userTask.get();
                Page<PostVO> postVOPage = postTask.get();
                Page<Picture> picturePage = pictureTask.get();
                searchVO.setUserList(userVOPage.getRecords());
                searchVO.setPostList(postVOPage.getRecords());
                searchVO.setPictureList(picturePage.getRecords());
            } catch (Exception e) {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR, "查询用户数据失败");
            }
        } else {
            switch (searchTypeEnum) {
                case POST:
                    Page<PostVO> postVOPage = postDataSource.doSearch(searchText, current, pageSize);
                    searchVO.setPostList(postVOPage.getRecords());
                    break;
                case USER:
                    Page<UserVO> userVOPage = userDataSource.doSearch(searchText, current, pageSize);
                    searchVO.setUserList(userVOPage.getRecords());
                    break;
                case PICTURE:
                    Page<Picture> picture = pictureDataSource.doSearch(searchText, current, pageSize);
                    searchVO.setPictureList(picture.getRecords());
                    break;
                default:
            }
        }
        return searchVO;
    }
}
